﻿<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<ProductInputModel>" %>

<%@ Import Namespace="SDT.Lib.Helpers" %>
<%@ Import Namespace="SDT.Admin.ViewModels" %>
<%@ Import Namespace="SDT.Model"%>
<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
    Sửa sản phẩm
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
    <%if (ViewData["MessageInfo"] == null)
      { %>
    <% using (Html.BeginForm("Edit", "Product", FormMethod.Post, new { id = "adminForm",enctype = "multipart/form-data"}))
       {%>
      <div class="bg_tabview">
    <input id="hdnSelected" value="#tab1" type="hidden" />
        <ul class="tabs">
            <li><a href="#tab1">Thông tin sản phẩm</a></li>
            <li><a href="#tab2">Thông tin mua theo nhóm</a></li>
        </ul>
    <div class="tab_container">	
	    <div id="tab1" class="tab_content"> 
    <table class="admintable" width="100%">
        <tbody>            
            <tr>
                <td class="key">
                    Loại sản phẩm (<font color="red">*</font>)
                </td>
                <td>
                    <%=Html.DropDownList("ddlCategory", new SelectList(Model.Categories, "ID", "Name",Model.Product.CatID), "-- Loại sản phẩm --", new { @class = "required", style = "width:200px" })%>
                </td>
            </tr>
            <tr>
		        <td class="key">Đối tượng của sản phẩm (<font color="red">*</font>)</td>
		        <td>
			        <ul class="list_rdo">
			        <%foreach (var item in Model.ProductTypes)
                    {
                        var has = Model.Product.ProductTypeMaps.Where(e => e.TypeID == item.ID).ToList().Count > 0 ? true : false;
                    %>
			            <li><%=Html.CheckBox("chkType", has, new { @Value = item.ID })%><%=Html.Encode(item.Name) %></li>
			        <%} %>
			        </ul>
		        </td>
	        </tr> 
	        <tr>
		        <td class="key">Trong dịp (<font color="red">*</font>)</td>
		        <td>
			        <ul class="list_rdo">
			        <%foreach (var item in Model.ProductHolidays)
                    {
                        var has = Model.Product.ProductHolidayMaps.Where(e => e.HolidayID == item.ID).ToList().Count > 0 ? true : false;
                    %>
			            <li><%=Html.CheckBox("chkHoliday", has, new { @Value = item.ID })%><%=Html.Encode(item.Name) %></li>
			        <%} %>
			        </ul>
		        </td>
	        </tr> 
            <tr>
                <td class="key">
                    Tên sản phẩm (<font color="red">*</font>)
                </td>
                <td>
                    <%=Html.TextBoxFor(m=>m.Product.Name, new { style = "width: 500px;", @class="required title" })%>
                </td>
            </tr>            
            <tr>
                <td class="key">
                    Mô tả ngắn (<font color="red">*</font>)
                </td>
                <td>
                    <%=Html.TextAreaFor(m => m.Product.Summary, new { @class = "required title", style = "height:100px" })%>
                </td>
            </tr>

            <tr>
                <td class="key">
                    Ảnh đại diện
                </td>
                <td>
                    <%if (Model.Product.ImagePath != null && Model.Product.ImagePath != string.Empty)
                      { %>
                    <img width="110" height="75" style="border: 1px solid #CCCCCC;" class="bor_img" alt=""
                        src="<%=string.Format("{0}{1}_{2}",Model.ImageRootUrl,Model.Product.ID,Model.Product.ImagePath) %> " />
                      <a href="/product/delimg/<%=Model.Product.ID %>">Xóa</a>    
                     <%} %>
                    <input type="file" name="ImagePaths" id="ImagePaths" />
                </td>
            </tr>
            <tr>
                <td class="key">
                  Thông tin mua 
                </td>
                <td>
                   Số lượng mua: <%=Html.TextBoxFor(m => m.Product.BuyQuantity, new { @class = "required number", style = "width: 50px; text-align: center;" })%>
                   Đơn giá mua: <%=Html.TextBox("txtBuyUnitPrice",string.Format("{0:0,0.0}",Model.Product.BuyUnitPrice), new { @class = "required number", style = "width: 120px; text-align: center;" })%> (VNĐ)
                </td>
            </tr>            
             <tr>
                <td class="key">
                    Thông tin bán ra
                </td>
                <td>
                    Số lượng còn lại: <%=Html.TextBoxFor(m=>m.Product.Quantity, new {@class = "required title", style = "width: 50px; text-align: center;" })%>
                    Đơn giá bán:  <%=Html.TextBox("txtUnitPrice", string.Format("{0:0,0.0}", Model.Product.UnitPrice), new { @class = "required number", style = "width: 120px; text-align: center;" })%> (VNĐ)
                    Đơn giá bán cũ:<%=Html.TextBox("txtUnitOldPrice", string.Format("{0:0,0.0}", Model.Product.UnitOldPrice), new { @class = "number", style = "width: 120px; text-align: center;" })%> (VNĐ)
                </td>
            </tr>
            <tr>
                <td class="key">
                    Giảm giá
                </td>
                <td>
                    <%=Html.TextBox("txtDiscountPercent", string.Format("{0:0,0.0}", Model.Product.DiscountPercent), new { @class = "number", style = "width: 50px; text-align: center;" })%> (%)
                     Số tiền được giảm giá:
                     <%=Html.TextBox("txtDiscountValue", string.Format("{0:0,0.0}", Model.Product.DiscountValue), new { @class = "number", style = "width: 120px; text-align: center;" })%>(VNĐ)
                </td>
            </tr> 
            <tr>
                <td class="key">
                    Phí chuyển hàng
                </td>
                <td>
                    <%=Html.TextBox("txtShipping", string.Format("{0:0,0.0}", Model.Product.FreeShipping), new { @class = "number", style = "width: 50px; text-align: center;" })%> (VNĐ)
                </td>
            </tr>                    
            <tr>
                <td class="key">
                    Kích thước
                </td>
                <td>
                    <%=Html.TextBoxFor(m => m.Product.Size, new { @class = "title" })%>
                </td>
            </tr>
             <tr>
                <td class="key">
                    Vật liệu
                </td>
                <td>
                    <%=Html.TextBoxFor(m => m.Product.Material, new { @class = "title" })%>
                </td>
            </tr>
            <tr>
                <td class="key">
                    Trạng thái hiển thị
                </td>
                <td>
                    <%= Html.RadioButton("Product.PubYN", true, Model.Product.PubYN)%>
                    Đăng tin
                    <%= Html.RadioButton("Product.PubYN", false, !Model.Product.PubYN)%>
                    Không đăng
                </td>
            </tr>
            <tr>
                <td class="key">
                    Sản phẩm nổi bật?
                </td>
                <td>
                    <%= Html.RadioButton("Product.HotYN", true, Model.Product.HotYN)%>
                    Tin hot
                    <%= Html.RadioButton("Product.HotYN", false , !Model.Product.HotYN)%>
                    Tin không hot
                </td>
            </tr>
            <tr>
                <td class="key">
                    Sản phẩm mới?
                </td>
                <td>
                    <%= Html.RadioButton("Product.NewYN", true, Model.Product.NewYN)%>
                    Mới
                    <%= Html.RadioButton("Product.NewYN", false, !Model.Product.NewYN)%>
                    Không mới
                </td>
            </tr>
            <tr>
                <td class="key">
                    Sản phẩm giảm giá?
                </td>
                <td>
                    <%= Html.RadioButton("Product.SaleYN", true, Model.Product.SaleYN)%>
                    Đúng
                    <%= Html.RadioButton("Product.SaleYN", false, !Model.Product.SaleYN)%>
                    Không đúng
                </td>
            </tr>
            <tr>
                <td class="key">
                    Sản phẩm khuyến mại?
                </td>
                <td>
                    <%= Html.RadioButton("Product.PromotionYN", true, Model.Product.PromotionYN)%>
                    Khuyến mại
                    <%= Html.RadioButton("Product.PromotionYN", false, !Model.Product.PromotionYN)%>
                    Không khuyến mại
                </td>
            </tr>
            <tr>
                <td class="key">
                    Ảnh đính kèm:
                </td>
                <td>
                    <table id="frmFile">
                        <% if (Model.ListFile != null && Model.ListFile.Count() > 0)
                           {
                           %><tr>
                               <td>
                                   <%
                                       foreach (string item in Model.ListFile)
                                       {
                                           var imgName = item.Substring(item.LastIndexOf("/") + 1);
                                       %>
                                   <img width="110" height="75" style="border: 1px solid #CCCCCC;" class="bor_img" alt=""
                                       src="<%= string.Format("{0}{1}",Model.ImageRootUrls,item)%>" />
                                       <a href="/product/deleteimg/<%=Model.Product.ID %>?img=<%=imgName %>">Xóa</a>
                                   <%}%>
                               </td>
                           </tr>
                        <%
                            }%>
                        <tr>
                            <td>
                                <input id="fileInput1" size="25" name="fileInput1" type="file" onchange="check_extension(this.value,'btnSave');" />
                                &nbsp;<input id="btnAddFile" type="button" value="Thêm" onclick="AddFile();" />
                            </td>
                        </tr>
                    </table>

                    <script type="text/javascript">
                        var fileIndex = 1;
                        var hash = { '.jpg': 1, '.png': 1, '.gif': 1, '.jpeg': 1 };
                        function check_extension(filename, submitId) {
                            var re = /\..+$/;
                            var ext = filename.match(re).toString().toLowerCase();
                            var submitEl = document.getElementById(submitId);
                            if (hash[ext]) {
                                submitEl.disabled = false;
                                return true;
                            } else {
                            }
                        };
                        function AddFile() {
                            fileIndex++;
                            var tag = "<td><input id='fileInput" + fileIndex.toString() + "' name='fileInput" + fileIndex.toString() + "' type=\"file\" size='25'  onchange=\"check_extension(this.value,'btnSave');\" /></td>";
                            var row = "<tr>" + tag + "</tr>";
                            $("#frmFile").append(row);
                        };
                    </script>

                </td>
            </tr>
            <tr>
                <td class="key">
                    Xuất sứ
                </td>
                <td>
                    <%=Html.DropDownList("ddlCountry", new SelectList(Model.Countries, "ID", "CountryName",Model.Product.CountryID.HasValue? Model.Product.CountryID.Value.ToString():""), "-- Quốc gia --", new { style = "width:200px" })%>
                </td>
            </tr>
            <tr>
                <td class="key">
                    Công ty cung cấp
                </td>
                <td>
                    <%=Html.DropDownList("ddlProvider", new SelectList(Model.Providers, "ID", "Name", Model.Product.ProviderID.HasValue? ""+Model.Product.ProviderID.Value:""), "-- Công ty cung cấp --", new { style = "width:200px" })%>
                </td>
            </tr>
            <tr>
                <td class="key">
                    Công ty sản xuất
                </td>
                <td>
                    <%=Html.DropDownList("ddlManufacturer", new SelectList(Model.Manufacturers, "ID", "Name",Model.Product.ManufactureID.HasValue? Model.Product.ManufactureID.Value.ToString():""), "-- Công ty sản xuất --", new { style = "width:200px" })%>
                </td>
            </tr>
            <tr>
                <td class="key">
                    Công ty bảo hành
                </td>
                <td>
                    <%=Html.DropDownList("ddlInsurer", new SelectList(Model.Insurers, "ID", "Name",Model.Product.InsurerID.HasValue? Model.Product.InsurerID.Value.ToString():""), "-- Công ty bảo hành --", new { style = "width:200px" })%>
                </td>
            </tr>
            <tr>
		        <td class="key">Trong sự kiện</td>
		        <td>
			        <div style="overflow:hidden">
			            <ul class="list_rdo">
			            <%foreach (var item in Model.Events)
                        {
                            var has = Model.Product.ProductEventMaps.Where(e => e.EventID == item.ID).ToList().Count > 0 ? true : false;
                        %>
			                <li><%=Html.CheckBox("chkEvent", has, new { @Value = item.ID })%><%=Html.Encode(item.Title) %></li>
			            <%} %>
			            </ul>
			        </div>
		        </td>
	        </tr>
            <tr>
                <td class="key">
                    Mô tả sản phẩm
                </td>
                <td>
                    <%=Html.TextAreaFor(m => m.Product.Description, new { style = "width:700px; height:350px" })%>
                </td>
            </tr>
             <tr>
                <td class="key">Chọn tag</td>
                <td>
                    <%
                        var tagName = string.Empty;
                        foreach (var item in Model.Product.ProductTagMaps) { tagName += item.Tag.Keyword+", "; } %>
                    <%= Html.TextBox("TagName", tagName.TrimEnd(','), new { style = "width:350px" })%>
                    <%= Html.Hidden("TagID")%> (<font color="red">*</font>)Cho phép chọn nhiều tag, mỗi tag cách nhau bằng dấu ",". Tag chưa có thì viết tên tag vào textbox 
                </td> 
            </tr>
            <tr>
                <td class="key">
                    Metadescription
                </td>
                <td>
                    <%=Html.TextAreaFor(m=>m.Product.MetaDescription, new { style="width:700px; height:50px" })%>
                </td>
            </tr>
            <tr>
                <td class="key">
                    Metakeyword
                </td>
                <td>
                    <%=Html.TextAreaFor(m => m.Product.MetaKeyword, new { style = "width:700px; height:50px" })%>
                </td>
            </tr>
            <tr>
                <td class="key">
                    Metatag
                </td>
                <td>
                    <%=Html.TextAreaFor(m => m.Product.MetaTag, new { style = "width:700px; height:50px" })%>
                </td>
            </tr>
        </tbody>
    </table>
    </div>
        <div id="tab2" class="tab_content">
            <table class="admintable" width="100%">
                <tbody>            
                    <tr>
                        <td class="key">
                            Ngày bắt đầu bán
                        </td>
                        <td>
                            <%= Html.TextBox("txtFromDate", Model.Product.BeginDate.HasValue ? Model.Product.BeginDate.Value.ToString("dd/MM/yyyy") : string.Empty, new { @class = "datepicker", style = "width:120px" })%>
                        </td>
                    </tr>
                    <tr>
                        <td class="key">
                            Ngày kết thúc bán
                        </td>
                        <td>
                            <%=string.Format("{0:dd/MM/yyyy}",Model.Product.EndDate)%>, Gia hạn thêm
                            <%=Html.DropDownList("ddlExpireDate", new SelectList(Model.ProductExpires, "Key", "Value").ToList(), "-- Ngày hết hạn --", new { style="width:120px"})%>
                        </td>
                    </tr>
                    <tr>
                        <td class="key">
                            Số phiếu tối thiểu
                        </td>
                        <td>
                            <%= Html.TextBoxFor(m=>m.Product.MinVoucher, new { @class = "number", style = "width:120px" })%>
                        </td>
                    </tr>
                    <tr>
                        <td class="key">
                            Số phiếu tối đa
                        </td>
                        <td>
                            <%= Html.TextBoxFor(m=>m.Product.MaxVoucher, new { @class = "number", style = "width:120px" })%>
                        </td>
                    </tr>
                    <tr>
                        <td class="key">
                            Điểm nổi bật
                        </td>
                        <td>
                            <%=Html.TextAreaFor(m => m.Product.Highlights, new { style = "width:700px; height:200px" })%>
                        </td>
                    </tr>
                    <tr>
                        <td class="key">
                            Điều kiện mua hàng
                        </td>
                        <td>
                            <%=Html.TextAreaFor(m => m.Product.Conditions, new { style = "width:700px; height:200px" })%>
                        </td>
                    </tr>
               </tbody>
            </table>        
        </div>
        <div class="clear"></div>
        <p style="text-align:center">
           <input type="submit" id="btnSave" value="Lưu dữ liệu" onclick="return check_extension('ImagePaths','btnSave');" />
        </p>
    </div>
    </div>
    <%}
} %>

    <script type="text/javascript">
        $(document).ready(function() {
            $("#adminForm").validate();
            $.datepicker.setDefaults($.datepicker.regional['vi']);
            $(".datepicker").datepicker({
                dateFormat: "dd/mm/yy",
                changeMonth: true,
                showOn: "button",
                buttonImage: "/Images/calendar.gif",
                buttonImageOnly: true,
                changeYear: true
            });
            $(".datepicker").mask("99/99/9999");
        });

        var fileIndex = 1;
        var hash = { '.jpg': 1, '.png': 1, '.gif': 1, '.doc': 1, '.docx': 1, '.xls': 1, '.xlsx': 1, '.zip': 1, '.rar': 1, '.ppt': 1, '.pptx': 1, '.txt:': 1 };
        function check_extension(filename, submitId) {
            var re = /\..+$/;
            var ext = filename.match(re).toString().toLowerCase();
            var submitEl = document.getElementById(submitId);
            if (hash[ext]) {
                submitEl.disabled = false;
                return true;
            } else {
            }
        }
        tinyMCE.init({
            mode: "exact",
            elements: "Product_Description",
            theme: "advanced",            
            plugins: "spellchecker,pagebreak,style,layer,table,save,advhr,advimage,advlink,emotions,iespell,inlinepopups,insertdatetime,preview,media,searchreplace,print,contextmenu,paste,directionality,fullscreen,noneditable,visualchars,nonbreaking,xhtmlxtras,template,paste,imagemanager",
            theme_advanced_buttons1: "save,newdocument,|,bold,italic,underline,strikethrough,|,justifyleft,justifycenter,justifyright,justifyfull,|,styleselect,formatselect,fontselect,fontsizeselect",
            theme_advanced_buttons2: "cut,copy,paste,pastetext,pasteword,|,search,replace,|,bullist,numlist,|,outdent,indent,blockquote,|,undo,redo,|,link,unlink,anchor,image,cleanup,help,code,|,insertdate,inserttime,preview,|,forecolor,backcolor",
            theme_advanced_buttons3: "tablecontrols,|,hr,removeformat,visualaid,|,sub,sup,|,charmap,emotions,iespell,media,advhr,|,print,|,ltr,rtl,|,fullscreen",
            theme_advanced_buttons4: "insertlayer,moveforward,movebackward,absolute,|,styleprops,spellchecker,|,cite,abbr,acronym,del,ins,attribs,|,visualchars,nonbreaking,template,blockquote,pagebreak,|,insertfile,insertimage",
            theme_advanced_toolbar_location: "top",
            theme_advanced_toolbar_align: "left",
            theme_advanced_statusbar_location: "bottom",
            paste_use_dialog: false,
            paste_auto_cleanup_on_paste: true,
            paste_convert_headers_to_strong: false,
            paste_strip_class_attributes: "all",
            paste_remove_spans: true,
            paste_remove_styles: true
        });
        $("#TagName").autocomplete({
            minLength: 2,
            source: function(request, response) {
                $.ajax({
                    url: "/AjaxData/SearchTag",
                    dataType: "json",
                    data: {
                        keyword: extractLast(request.term)
                    },
                    success: function(data) {
                        response($.map(data, function(item) {
                            return {
                                label: item.Keyword,
                                value: item.Keyword,
                                id: item.ID
                            }
                        }));
                    }
                });
            },
            focus: function() {
                // prevent value inserted on focus
                return false;
            },
            select: function(event, ui) {
                var terms = split(this.value);
                // remove the current input
                terms.pop();
                // add the selected item
                terms.push(ui.item.value);
                // add placeholder to get the comma-and-space at the end
                terms.push("");
                this.value = terms.join(", ");

                var termsk = split($('#TagID').val());
                // remove the current input
                termsk.pop();
                // add the selected item
                termsk.push(ui.item.id);
                // add placeholder to get the comma-and-space at the end
                termsk.push("");
                $('#TagID').val(termsk.join(","));
                return false;
            }
        });
    </script>
    <script type="text/javascript">
        $(document).ready(function() {
            var selectedTab = $('input[id$="hdnSelected"]').val();
            $(".tab_content").hide(); //Hide all content
            //$("ul.tabs li:first").addClass("active").show(); //Activate first tab
            //$(".tab_content:first").show(); //Show first tab content
            $("a[href=" + "" + selectedTab + "" + "]").parent().addClass("active");
            $(selectedTab).show();
            //On Click Event
            $("ul.tabs li").click(function() {

                $("ul.tabs li").removeClass("active"); //Remove any "active" class
                $(this).addClass("active"); //Add "active" class to selected tab
                $(".tab_content").hide(); //Hide all tab content
                var activeTab = $(this).find("a").attr("href"); //Find the href attribute value to identify the active tab + content
                $(activeTab).fadeIn(); //Fade in the active ID content
                $('input[id$="hdnSelected"]').val(activeTab);
                return false;
            });
        });
  </script>
</asp:Content>
<asp:Content ID="Content3" ContentPlaceHolderID="Toolbar" runat="server">
    <%= Html.Toolbar("Quay về", "/Product", "back") %>
</asp:Content>
<asp:Content ID="Content4" ContentPlaceHolderID="Header" runat="server">
    Sửa sản phẩm
</asp:Content>
<asp:Content ID="Content5" ContentPlaceHolderID="Message" runat="server">
    <%=ViewData["MessageInfo"]!=null? "<p class='perror'>"+Html.Encode(ViewData["MessageInfo"])+"</p>":string.Empty%>
</asp:Content>
